﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ServiceUsers : AppServices
{
    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        if (!Page.IsPostBack)
        {
            string serviceName = Request["name"] != null ? Request["name"].ToString() : SrvDetails.ServiceName;
            string serviceDetail = Request["description"] != null ? Request["description"].ToString() : SrvDetails.ServiceDescription;
            ProxyWrapper.ServicesServiceWrapper _srvWrapper = new ProxyWrapper.ServicesServiceWrapper();
            _srvWrapper.UpdateService(UserDetails, ServiceId, serviceName, serviceDetail);
            //Response.Redirect("", false);
        }
    }
    protected string LoadUserList()
    {
        try
        {
            int i = 0;
          //  ProxyWrapper.ServicesServiceWrapper _services = new ServicesServiceWrapper();
            ProxyWrapper.UserServiceWrapper _usr = new ProxyWrapper.UserServiceWrapper();
            Dictionary<int, string> userData = new Dictionary<int, string>();
            Dictionary<int, string> serviceUserData = new Dictionary<int, string>();
            DataSet dsUsers = _usr.GetUsers(UserDetails.ClientId, UserDetails.HashCode);
            if (dsUsers != null && dsUsers.Tables.Count > 0 && dsUsers.Tables[0].Rows.Count > 0)
            {
                for (int k = 0; k < dsUsers.Tables[0].Rows.Count; k++)
                {
                    userData.Add(Convert.ToInt32(dsUsers.Tables[0].Rows[k][0]), Convert.ToString(dsUsers.Tables[0].Rows[k][1]));
                }

            }

            if (SrvDetails != null && SrvDetails.GetUserList != null && SrvDetails.GetUserList.Tables.Count > 0 && SrvDetails.GetUserList.Tables[0].Rows.Count > 0)
            {
                for (int j = 0; j < SrvDetails.GetUserList.Tables[0].Rows.Count; j++)
                {
                    serviceUserData.Add(Convert.ToInt32(SrvDetails.GetUserList.Tables[0].Rows[j][0]), Convert.ToString(SrvDetails.GetUserList.Tables[0].Rows[j][1]));
                }

            }
            string elemnts = "<li><input type='checkbox' id='inputee_{0}' class='inputcheckbox' name='selectedUsers[]' value='{1}' {2} /><label class='selectbox_label' for='inputee_{3}'>{4}</label>"
                            + "     <a title='Edit {5}' target='_blank' href='CreateUser.aspx?userid={6}'> <img style=margin: 0px 0px 10px 2px; src=Images/external_link.gif></a></li>";
            object[] args = null;
            StringBuilder sb = new StringBuilder();
            foreach (KeyValuePair<int, string> item in userData)
            {
                args = new object[7];
                args[0] = i;
                args[1] = item.Key;
                if (serviceUserData != null && serviceUserData.Count > 0)
                {
                    if (serviceUserData.ContainsKey(item.Key))
                        args[2] = "checked";
                    else
                        args[2] = "";

                }
                else
                    args[2] = "";

                args[3] = i;
                args[4] = item.Value;
                args[5] = item.Value;
                args[6] = item.Key;
                i++;
                sb.Append(string.Format(elemnts, args));
            }
            return sb.ToString();
        }
        catch (Exception)
        {
            return "No Service Found";
        }
    }
}